package ar.com.pragmatia.lanacion.data;

import java.util.Vector;

import android.content.ContentValues;
import ar.com.pragmatia.lanacion.app.App;
import ar.com.pragmatia.lanacion.utils.Strings;

public class Order extends AppData implements Datable {

	public static final String TABLE_NAME = "orders";
	public static final String ID = "";
	public static final String FIELDS[] = { "id", "seller_id", "client_id",
			"created", "modified", "sent", "location", "order_type",
			"document_type" };

	private String sellerId;
	private String clientId;
	private String deliveryDate;
	private String created;
	private String modified;
	private String documentType;
	private String orderType;
	private String sent;
	private String location;
	private Client client;
	private Vector<AppData> orderDetail;

	public Order() {
		super();
		sent = "false";
	}

	public String getDocumentType() {
		return documentType;
	}

	public void setDocumentType(String documentType) {
		this.documentType = documentType;
	}

	public String getOrderType() {
		return orderType;
	}

	public void setOrderType(String orderType) {
		this.orderType = orderType;
	}

	public Order(int id, String sellerId, String clientId, String deliveryDate,
			String created, String modified) {
		super();
		this.sellerId = sellerId;
		this.clientId = clientId;
		this.deliveryDate = deliveryDate;
		this.created = created;
		this.modified = modified;
	}

	public String getSellerId() {
		return sellerId;
	}

	public String getLocation() {
		return location;
	}

	public void setLocation(String location) {
		this.location = location;
	}

	public void setSellerId(String sellerId) {
		this.sellerId = sellerId;
	}

	public String getClientId() {
		return clientId;
	}

	public void setClientId(String clientId) {
		this.clientId = clientId;
	}

	public String getDeliveryDate() {
		return deliveryDate;
	}

	public void setDeliveryDate(String deliveryDate) {
		this.deliveryDate = deliveryDate;
	}

	public String getCreated() {
		return created;
	}

	public void setCreated(String created) {
		this.created = created;
	}

	public String getModified() {
		return modified;
	}

	public void setModified(String modified) {
		this.modified = modified;
	}

	public Vector<AppData> getOrderDetail() {
		if (orderDetail == null) {
			orderDetail = new OrderDetail().find("WHERE order_id = '" + getId()
					+ "'");
		}
		return orderDetail;
	}

	public void setOrderDetail(Vector<AppData> orderDetail) {
		this.orderDetail = orderDetail;
	}

	public Client getClient() {
		if (client == null) {
			client = (Client) new Client().find(
					"WHERE id = '" + getClientId() + "'").get(0);
		}
		return client;
	}

	public boolean save(boolean flag) {

		if (flag) {
			setId(getInsertId());
			setSellerId(App.getUser().getId());
			ContentValues values = new ContentValues();

			for (int i = 0; i < FIELDS.length; i++) {

				String value = null;
				try {
					value = executeStringGetterMethod(this.getClass(), "get"
							+ Strings.camelize(FIELDS[i], false));
				} catch (NoSuchMethodException e) {
					// e.printStackTrace();
					// App.log("AppData", "save", this.getClass().getName());
					App.log("AppData", "save", e.getMessage());
				}

				if (FIELDS[i].equals("id") && Strings.isNullOrEmpty(value)) {
					continue;
				} else {
					value = (Strings.isNullOrEmpty(value)) ? "" : value;
					values.put(FIELDS[i], value);
				}

			}

			long r = -1;
			try {
				r = App.getDb().insert(getTableName(), null, values);
			} catch (Exception e) {
				e.printStackTrace();
				App.log("AppData", "save", e.getMessage());
			}
			return (r >= 0) ? true : false;
		}
		return false;
	}

	private String getInsertId() {
		String id = "10000";
		Vector<AppData> v = this.find("WHERE sent = 'false' ORDER BY id DESC");
		if (v.size() > 0) {
			id = (new Integer(v.get(0).getId()) + 1) + "";
		}
		return id;
	}

	public void setClient(Client client) {
		this.client = client;
	}

	public String getSent() {
		return sent;
	}

	public void setSent(String sent) {
		this.sent = sent;
	}

}
